﻿using MyUAMS.BLL;
using MyUAMS.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserWork_UpdataAmount : System.Web.UI.Page
{
    WorkServer assetSrv = new WorkServer();
    int assetId;
    string assetname = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            txtSearch.Attributes["placeholder"] = "搜索";
            if (Request.QueryString["SearchText"] != null)
            {
                string strSearchText = Request.QueryString["SearchText"].ToString();
                dgvAsset.DataSource = assetSrv.Search(strSearchText);
                dgvAsset.DataBind();
                if (strSearchText.Count() == 0)
                {
                    lblProErr.Text = "未搜索到数据";
                }
            }

            else
            {
                Bind();
            }
            APBind();
            
        }

    }

    protected void return_Click(object sender, EventArgs e)
    {
        Response.Redirect("../UserWork/MyWork.aspx");
    }
    protected string GetCategoryName(object categoryId)
    {
        if (categoryId == null) return string.Empty; // 防止空值

        int id = Convert.ToInt32(categoryId); // 将对象转换为整数

        using (var context = new DataClasses1DataContext())
        {
            // 查询类别名称
            var category = context.AssetType_Table.SingleOrDefault(c => c.Tid == id);
            return category != null ? category.Tname : "未知类别"; // 返回类别名称或默认值
        }
    }
    protected void Bind()
    {
        var products = assetSrv.GetAllProduct();
        if (products.Count() == 0)
        {
            lblProErr.Text = "校园还未拥有资产,请添加！";
        }
        else
        {
            lblProErr.Text = " ";
        }
        dgvAsset.DataSource = assetSrv.GetAllProduct();
        dgvAsset.DataBind();
        
    }

    protected void APBind()
    {
        if(Request.QueryString["APid"] != null)
        {
            dgvAsset.Visible = false;
            int pid = int.Parse(Request.QueryString["APid"]);
            var APTable = assetSrv.GetAPByAPid(pid);
            txtVid.Text = APTable.VoucherId.ToString();
            txtAmount.Text = APTable.Aamount.ToString();
            txtRemark.Text = APTable.Remark.ToString();
        }
        else
        {
            return;
        }
        

       
    }
    protected void Search_Asset(object sender, EventArgs e)
    {
        string strQuery = "";
        if (txtSearch.Text.Trim() == "")
        {
            strQuery = "";
        }
        else
        {
            strQuery = "?SearchText=" + txtSearch.Text.Trim();
        }
        Response.Redirect("../UserWork/UpdataAmount.aspx" + strQuery);
    }


    protected void btnIntput_Click(object sender, EventArgs e)
    {
        if(Request.QueryString["APid"] != null)
        {
            if (Session["CustomerId"] != null && Session["CustomerName"] != null)
            {
                int uid = (int)Session["CustomerId"];
                string username = Session["CustomerName"].ToString();
                int pid = int.Parse(Request.QueryString["APid"]);
                var APTable = assetSrv.GetAPByAPid(pid);
                int assetId = APTable.Aid;
                string assetname = APTable.Aname;
                assetSrv.Insert_AP(assetId, assetname, "入库", int.Parse(txtVid.Text.Trim()), int.Parse(txtAmount.Text.Trim()), uid, username, txtRemark.Text.Trim());
                lblProErr.Text = "入库成功";
                assetSrv.DeleteAPByAPid(pid);
            }
        }
        else
        {
            GridView gvProduct = new GridView();
            gvProduct = (GridView)Page.FindControl("dgvAsset");
            if (gvProduct != null)
            {
                for (int i = 0; i < gvProduct.Rows.Count; i++)
                {
                    CheckBox chkChoice = new CheckBox();
                    chkChoice = (CheckBox)gvProduct.Rows[i].FindControl("chkChoice");
                    if (chkChoice != null)
                    {
                        if (chkChoice.Checked)
                        {
                            assetId = int.Parse(gvProduct.Rows[i].Cells[1].Text);
                            assetname = gvProduct.Rows[i].Cells[2].Text;
                            if (Session["CustomerId"] != null && Session["CustomerName"] != null)
                            {
                                int uid = (int)Session["CustomerId"];
                                string username = Session["CustomerName"].ToString();
                                assetSrv.Insert_AP(assetId, assetname, "入库", int.Parse(txtVid.Text.Trim()), int.Parse(txtAmount.Text.Trim()), uid, username, txtRemark.Text.Trim());
                                lblProErr.Text = "入库成功";
                            }
                        }
                    }
                    else
                    {
                        lblProErr.Text = "请选择需要出入库的资产";
                    }
                }
            }
        }
        
        Response.Redirect("../UserWork/UpdataAmount.aspx");

    }
    protected void btnOutput_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["APid"] != null)
        {
            if (Session["CustomerId"] != null && Session["CustomerName"] != null)
            {
                int uid = (int)Session["CustomerId"];
                string username = Session["CustomerName"].ToString();
                int pid = int.Parse(Request.QueryString["APid"]);
                var APTable = assetSrv.GetAPByAPid(pid);
                int assetId = APTable.Aid;
                string assetname = APTable.Aname;
                assetSrv.Insert_AP(assetId, assetname, "出库", int.Parse(txtVid.Text.Trim()), int.Parse(txtAmount.Text.Trim()), uid, username, txtRemark.Text.Trim());
                lblProErr.Text = "出库成功";
                assetSrv.DeleteAPByAPid(pid);
            }
        }
        else
        {
            GridView gvProduct = new GridView();
            gvProduct = (GridView)Page.FindControl("dgvAsset");
            if (gvProduct != null)
            {
                for (int i = 0; i < gvProduct.Rows.Count; i++)
                {
                    CheckBox chkChoice = new CheckBox();
                    chkChoice = (CheckBox)gvProduct.Rows[i].FindControl("chkChoice");
                    if (chkChoice != null)
                    {
                        if (chkChoice.Checked)
                        {
                            assetId = int.Parse(gvProduct.Rows[i].Cells[1].Text);
                            assetname = gvProduct.Rows[i].Cells[2].Text;
                            if (Session["CustomerId"] != null && Session["CustomerName"] != null)
                            {
                                int uid = (int)Session["CustomerId"];
                                string username = Session["CustomerName"].ToString();
                                assetSrv.Insert_AP(assetId, assetname, "出库", int.Parse(txtVid.Text.Trim()), int.Parse(txtAmount.Text.Trim()), uid, username, txtRemark.Text.Trim());
                                lblProErr.Text = "出库成功";
                            }
                        }
                    }
                    else
                    {
                        lblProErr.Text = "请选择需要出入库的资产";
                    }
                }
                
            }
        }
        Response.Redirect("../UserWork/UpdataAmount.aspx");
    }

   
}
